Skip to content

feat(sui-indexer): maintain per-network presign pool for redeems#319

Merged
sczembor merged 10 commits intomasterfrom
stan/presign-queue
Feb 3, 2026
Merged

feat(sui-indexer): maintain per-network presign pool for redeems#319
sczembor merged 10 commits intomasterfrom
stan/presign-queue

Conversation

@sczembor
Copy link
Contributor

@sczembor sczembor commented Jan 23, 2026

Description

Closes: #282


Author Checklist

All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.

I have...

  • included the correct type prefix in the PR title
  • added ! to the type prefix if API or client breaking change
  • added appropriate labels to the PR
  • provided a link to the relevant issue or specification
  • added a changelog entry to CHANGELOG.md
  • included doc comments for public functions
  • updated the relevant documentation or specification
  • reviewed "Files changed" and left comments if necessary

Summary by Sourcery

Maintain a pool of pre-created presign objects during the redeem solver cron job to ensure availability for active Sui networks.

New Features:

  • Add a redeem service method to refill and maintain a per-network pool of presign objects before processing redeems.
  • Introduce a storage query to count presign objects per Sui network for pool management.

Enhancements:

  • Update the redeem solver cron workflow to invoke presign pool refilling alongside existing redeem processing tasks.

Tests:

  • Add tests verifying correct counting of presign objects per network in storage.

Signed-off-by: sczembor <stanislaw.czembor@gmail.com>
@sczembor sczembor requested a review from a team as a code owner January 23, 2026 16:10
@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Jan 23, 2026

Reviewer's Guide

Implements proactive creation and pooling of Sui presign objects in the redeem cron job, adding storage support to count existing presigns and wiring the pool maintenance into the main redeem solver flow with tests.

Sequence diagram for proactive presign pool refill in redeem cron job

sequenceDiagram
    participant Runner as RedeemCron
    participant Service as RedeemService
    participant Storage as D1Storage
    participant SuiClient as SuiClient
    participant Logger as Logger

    Runner->>Service: refillPresignPool(activeNetworks)
    loop for each network in activeNetworks
        Service->>Storage: getPresignCount(network)
        Storage-->>Service: count
        alt count < PRESIGN_POOL_MIN
            Service->>Service: compute needed = PRESIGN_POOL_TARGET - count
            Service->>Service: toCreate = min(needed, MAX_CREATE_PER_RUN)
            Service->>Logger: debug F"Filling presign pool" with network, currentCount, creating
            loop for i from 0 to toCreate - 1
                Service->>Service: client = getSuiClient(network)
                Service->>SuiClient: requestIkaPresign()
                SuiClient-->>Service: presignId
                Service->>Storage: insertPresignObject(presignId, network)
                Service->>Logger: debug "Created presign object" with network, presignId
                opt error during creation
                    Service->>Logger: logError "Failed to create presign object" with method maintainPresignPool, network
                    Service->>Service: break
                end
            end
        else count >= PRESIGN_POOL_MIN
            Service->>Service: skip creation for network
        end
    end
Loading

Class diagram for RedeemService and D1Storage presign pool changes

classDiagram
    class RedeemService {
        +RedeemService(storage D1Storage, suiClients Map~SuiNet, SuiClient~)
        +refillPresignPool(activeNetworks SuiNet[]) Promise~void~
        +processPendingRedeems() Promise~void~
        +solveReadyRedeems() Promise~void~
        +broadcastReadyRedeems() Promise~void~
        +getSuiClient(network SuiNet) SuiClient
        -storage D1Storage
        -suiClients Map~SuiNet, SuiClient~
    }

    class D1Storage {
        +getPresignCount(network SuiNet) Promise~number~
        +popPresignObject(network SuiNet) Promise~string | null~
        +insertPresignObject(presignId string, network SuiNet) Promise~void~
        -db D1Database
    }

    class Logger {
        +debug(data any) void
    }

    class SuiClient {
        +requestIkaPresign() Promise~string~
    }

    RedeemService --> D1Storage : uses
    RedeemService --> SuiClient : uses
    RedeemService --> Logger : logs
    D1Storage --> D1Database : persists
Loading

File-Level Changes

Change Details Files
Add a presign pool refilling mechanism to the redeem service and wire it into the cron job run sequence.
  • Introduce presign pool configuration constants for target, minimum, and maximum creations per run.
  • Implement refillPresignPool to check current presign count per active network and create additional presign objects up to configured limits.
  • Use getSuiClient and requestIkaPresign to create presigns, persisting them via storage and logging success/failure with early exit on error.
  • Invoke refillPresignPool alongside existing redeem processing steps in runRedeemSolver and include it in centralized error reporting.
packages/sui-indexer/src/redeem-service.ts
packages/sui-indexer/src/index.ts
Extend storage with a presign object counting API and test coverage.
  • Add getPresignCount to D1Storage to count presign_objects per Sui network using a SQL COUNT query with a safe default.
  • Add unit test verifying getPresignCount returns correct counts for networks with multiple, single, and zero presign objects.
packages/sui-indexer/src/storage.ts
packages/sui-indexer/src/storage.test.ts

Possibly linked issues

  • #N/A: The PR implements a scheduled presign pool refiller in the redeem solver, matching the issue’s off-chain presign strategy.
  • #(unassigned / design discussion): PR implements worker-side precreation of presign objects via cron, directly addressing the issue’s presign process design

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sczembor
Copy link
Contributor Author

@sourcery-ai title

@sourcery-ai sourcery-ai bot changed the title feat: create presign objects up front in cron job feat(sui-indexer): maintain per-network presign pool for redeems Jan 23, 2026
Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 1 issue, and left some high level feedback:

  • In refillPresignPool, the error log uses method: "maintainPresignPool" which no longer matches the function name; consider updating this to refillPresignPool (or a shared constant) to keep logs consistent and searchable.
  • In getPresignCount, D1/SQLite often returns aggregate values as strings; to avoid subtle type issues, consider explicitly casting with Number(result?.count ?? 0) instead of relying on the inferred { count: number } type.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In `refillPresignPool`, the error log uses `method: "maintainPresignPool"` which no longer matches the function name; consider updating this to `refillPresignPool` (or a shared constant) to keep logs consistent and searchable.
- In `getPresignCount`, D1/SQLite often returns aggregate values as strings; to avoid subtle type issues, consider explicitly casting with `Number(result?.count ?? 0)` instead of relying on the inferred `{ count: number }` type.

## Individual Comments

### Comment 1
<location> `packages/sui-indexer/src/storage.ts:245-248` </location>
<code_context>
 	}

+	async getPresignCount(network: SuiNet): Promise<number> {
+		const result = await this.db
+			.prepare("SELECT COUNT(*) as count FROM presign_objects WHERE sui_network = ?")
+			.bind(network)
+			.first<{ count: number }>();
+		return result?.count || 0;
+	}
</code_context>

<issue_to_address>
**issue (bug_risk):** Be careful with the type of COUNT(*) results; they may come back as strings from the driver.

Since the driver may return `COUNT(*)` as a string, typing `first<{ count: number }>()` and then using `result?.count || 0` could hide a mismatch (e.g. `"0"`). Consider using `first<{ count: string | number }>()` and coercing with `Number(result?.count) || 0` to make the conversion explicit and robust to driver behavior.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

sczembor and others added 4 commits February 2, 2026 20:39
Signed-off-by: sczembor <stanislaw.czembor@gmail.com>
Signed-off-by: sczembor <stanislaw.czembor@gmail.com>
Signed-off-by: sczembor <stanislaw.czembor@gmail.com>
sczembor and others added 2 commits February 3, 2026 11:05
Signed-off-by: sczembor <stanislaw.czembor@gmail.com>
Copy link
Contributor

@robert-zaremba robert-zaremba left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pre-approving, we can handle the PTB in other PR if you want

Co-authored-by: Robert Zaremba <robert@zaremba.ch>
Signed-off-by: sczembor <43810037+sczembor@users.noreply.github.com>
@sczembor sczembor merged commit 1f50aad into master Feb 3, 2026
13 checks passed
@sczembor sczembor deleted the stan/presign-queue branch February 3, 2026 17:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

workers: fill presign objects

2 participants